﻿package CutLoad.components.loadbar
{
	import CutLoad.Base;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.*;

	/**
	 * 一个简单的实现CutLoad.components.ILoadBar接口的加载进度条    平滑过渡动画：无论加载的有多快，从1到100的动画，都要放一次
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class Loadbar extends Base implements ILoadBar
	{
		//宽
		protected var w:Number=100;
		//高
		protected var h:Number=10;
		//边框
		protected var lineThick:Number;
		protected var lineColor:Number;
		protected var lineAlpha:Number
		//填充
		protected var fillColor:Number
		protected var fillAlpha:Number
		//
		protected var txt:TextField;
		private var txtcolor:Number=0xffffff;
		private var txtW:Number=30;
		//
		protected var spKuang:Sprite;
		protected var spBar:Sprite;
		//
		protected var strTalkYou:String="";
		//
		protected var clone_w:Number;
		protected var clone_h:Number;
		protected var clone_fillColor:Number;
		protected var clone_txtsize:Number;
		protected var clone_txtcolor:Number;
		protected var clone_fillAlpha:Number;
		protected var clone_lineThick:Number;
		protected var clone_lineColor:Number;
		protected var clone_lineAlpha:Number;
		protected var clone_bakColor:Number;
		protected var clone_bakColorAlpha:Number;
		//
		protected var LoadCompleteDo:Function;
		//保存当前实际加载的百分比 1~100
		protected var currentHaveLoad:Number=0;
		//当前播放到的百分比 1~100
		protected var currentPlayLoad:Number=0;
		/**
		 * 返回一个只有显示文本提示进度的进度条 
		 * @param w
		 * @param h
		 * @param txtsize
		 * @param txtcolor
		 * @return 
		 * 
		 */		
		public static function getOnlyTxtLoadBar(w:Number=200,h:Number=20,txtsize:Number=10,txtcolor:Number=0x000000):Loadbar{
			return new Loadbar(w,h,0,txtsize,txtcolor,0,0,0,0,0,0);
		}
		/**
		 * 
		 * @param w 宽
		 * @param h 高度
		 * @param fillColor 进多条色
		 * @param txtsize 文字尺寸
		 * @param txtcolor 文字色
		 * @param fillAlpha 进多条色alpha
		 * @param lineThick 边框多粗
		 * @param lineColor 边框线色
		 * @param lineAlpha 边框色透明度
		 * @param bakColor 背景色
		 * @param bakColorAlpha 背景色透明度
		 *  模板：Loadbar(宽,高度,进多条色,文字尺寸,文字色,进多条色alpha,边框多粗,边框线色,边框色透明度,背景色,背景色透明度)
		 */		
		public function Loadbar(w:Number=200,h:Number=20,fillColor:Number=0xcccccc,txtsize:Number=10,txtcolor:Number=0x000000,fillAlpha:Number=1,lineThick:Number=1,lineColor:Number=0xcccccc,lineAlpha:Number=1,bakColor:Number=0xffffff,bakColorAlpha:Number=0.3)
		{
			
			clone_w=w;
			clone_h=h;
			clone_fillColor=fillColor
			clone_txtsize=txtsize
			clone_txtcolor=txtcolor
			clone_fillAlpha=fillAlpha
			clone_lineThick=lineThick
			clone_lineColor=lineColor
			clone_lineAlpha=lineAlpha
			clone_bakColor=bakColor;
			clone_bakColorAlpha=bakColorAlpha;
			//
			this.w=w;
			this.h=h;
			this.fillColor=fillColor;
			this.fillAlpha=fillAlpha;
			this.txtcolor=txtcolor;
			this.lineThick=lineThick;
			this.lineColor=lineColor;
			this.lineAlpha=lineAlpha;
			//
			spBar=new Sprite();
			this.addChild(spBar);
			spKuang=new Sprite();
			var gg:Graphics=spKuang.graphics;
			gg.lineStyle(lineThick,lineColor,lineAlpha);
			gg.beginFill(clone_bakColor,clone_bakColorAlpha);
			gg.drawRect(0,0,w,h);
			gg.endFill()
			this.addChild(spKuang);
			//
			var txtformat:TextFormat=new TextFormat;
			txtformat.align = TextFormatAlign.CENTER;
			txtformat.size=txtsize;
			txtformat.color=txtcolor;
			txtformat.bold=false;
			txtformat.font="Arial";
			//
			txt=new TextField();	
			//
			txt.autoSize=TextFieldAutoSize.CENTER;
			txt.width=w;
			txt.y=-Math.floor(txt.textHeight/2);
			txt.x=(w-txt.textWidth)/2
			txt.defaultTextFormat=txtformat;
			txt.setTextFormat(txtformat);
			this.addChild(txt);
			//
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			//
			dreawing()
		}
		//
		private function enterFrame(e:Event):void{
			//
			if(currentPlayLoad<this.currentHaveLoad){
				currentPlayLoad++;
				//trace(currentPlayLoad)
			}
			//显示出动画
			var gg:Graphics=spBar.graphics;
			gg.clear();
			gg.beginFill(fillColor,fillAlpha);
			gg.drawRect(0,0,w*(currentPlayLoad/100),h);
			gg.endFill();
			//
			txt.text=this.strTalkYou+String(currentPlayLoad)+"%";
			//
			if(currentPlayLoad>=100){
				//当进度为100时，处理LoadCompleteDo方法。
				if(LoadCompleteDo!=null) LoadCompleteDo();
				if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			}
		}
		override public function getShowWidth():Number{
			return this.w;
		}
		override public function getShowHeight():Number{
			return this.h
		}
		public function showHide(isShow:Boolean):void{
			this.visible=isShow;
			if(!isShow){
				currentHaveLoad=0
				currentPlayLoad=0;
			}
		}
		public function clone():ILoadBar{
			return new Loadbar(clone_w,clone_h,clone_fillColor,clone_txtsize,clone_txtcolor,clone_fillAlpha,clone_lineThick,clone_lineColor,clone_lineAlpha,clone_bakColor,clone_bakColorAlpha)
		}
		public function dreawing():void{
			
		}
		public function setValue(per:Number,strTalkYou:String):void{
			//trace("setValue:"+per+","+strTalkYou);
			currentHaveLoad=Math.floor(per*100);
			this.strTalkYou=strTalkYou;
		}
		/**
		 * 设置当加载完成后，要运行一个方法。 
		 * @param fun
		 * 
		 */		
		public function setLoadCompleteDo(fun:Function):void{
			this.LoadCompleteDo=fun;
			if(this.hasEventListener(Event.ENTER_FRAME)) this.removeEventListener(Event.ENTER_FRAME,enterFrame);
			this.addEventListener(Event.ENTER_FRAME,enterFrame);
			currentPlayLoad=0;
		}
		/**
		 * 当加载完成后，是否要运行一个方法
		 * @return 
		 * 
		 */		
		public function isLoadCompleteDo():Boolean{
			return true;
		}
		/**
		 * 加载完成时调用这个方法
		 * 
		 */				
		public function loadComplete():void{
			currentHaveLoad=100;
		}
	}
}